package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.example.demo.entity.User;
import com.example.demo.entity.query.UserQuery;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;

/**
 * 初次做SpringBoot，要解决页面跳转的问题，
 * 弄好后，其实也不算个事，写出来给大家提个醒！
 * 其实不要使用spring boot的@RestController注解，
 * 直接使用spring原来的注解@Controller就可以了。
 * @author MMCC
 *
 */
@Controller
public class UserContorller {

	@Autowired
	private UserService userService;
	
	@GetMapping("/index")
	public String GetIndex(Model model,UserQuery userQuery) {
		PageInfo<User> userPageInfo = userService.SelectByName(userQuery);
		model.addAttribute("page", userPageInfo);
	
		return "index";
	}
	
	@PostMapping("/index")
	public String PostIndex(Model model, UserQuery userQuery) {

		PageInfo<User> userPageInfo = userService.SelectByName(userQuery);
		model.addAttribute("page", userPageInfo);
		return "index";
	}
	
	@GetMapping("/deleteById")
	public String DeleteById(int id,RedirectAttributes attributes) {
	   boolean b=userService.DeleteById(id);
		if(b) {
	    	attributes.addFlashAttribute("message","删除成功");
	    }else {
	    	attributes.addFlashAttribute("message", "删除失败");
	    }
	    return "redirect:/index";
		
	}

	
	
}
